package com.zack.util.basic.type;

/**
 * email: miao.du@shuruitech.com author: zack time(default is Create Time may
 * change)：Jan 20, 2017 12:00:40 PM
 * 
 * 处理数字进制转换的
 **/
public class NumRelatedUtil {
    /**
     * @param num
     *            给定一个数
     * @param numberFor
     *            转换成几进制 例如传入16,2 就是说把16转换成2进制
     */
    public static int PNumber(int num, int numberFor) {
        if (num == 0) {
            return 0;
        } else {
            PNumber(num / numberFor, numberFor);
            // System.out.println(num % numberFor);
            return (num % numberFor);
        }
    }

    /**
     * 返回1 0 互换 但是不改变第一位的值 用于反码
     * 
     * @param abc
     * @return conver 1 and 0 in binary files
     */
    public static String zeroOneInverseFirst(String abc) {
        char kk = abc.charAt(0);
        String defString = abc.replaceAll("0", "2");
        String defString1 = defString.replaceAll("1", "0");
        String defString2 = defString1.replaceAll("2", "1");
        String defString3 = defString2.replaceAll("^[01]", (kk + ""));

        return defString3;
    }

    // 返回1 0 互换
    public static String zeroOneInverse(String abc) {
        String defString = abc.replaceAll("0", "2");
        String defString1 = defString.replaceAll("1", "0");
        String defString2 = defString1.replaceAll("2", "1");

        return defString2;
    }
}
